/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductMixDetail;
import com.chlitina.store.modules.expert.service.SolutionFaceProductMixDetailService;
import com.chlitina.store.modules.expert.service.SolutionFaceProductMixService;

/**
 *  方案产品步骤详细Controller
 * @author liujiang
 * @version 2013-08-19
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/expert/solutionFaceProductMixDetail")
public class SolutionFaceProductMixDetailController extends BaseController {

	@Autowired
	private SolutionFaceProductMixDetailService solutionFaceProductMixDetailService;
	@Autowired
	private SolutionFaceProductMixService solutionFaceProductMixService;
	
	@ModelAttribute
	public SolutionFaceProductMixDetail get(@RequestParam(required=false) Long id) {
		if (id != null){
			return solutionFaceProductMixDetailService.get(id);
		}else{
			return new SolutionFaceProductMixDetail();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(SolutionFaceProductMixDetail solutionFaceProductMixDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SolutionFaceProductMixDetail> page = solutionFaceProductMixDetailService.find(new Page<SolutionFaceProductMixDetail>(request, response), solutionFaceProductMixDetail); 
        model.addAttribute("page", page);
		return "modules/expert/solutionFaceProductMixDetailList";
	}

	@RequestMapping(value = "form")
	public String form(SolutionFaceProductMixDetail solutionFaceProductMixDetail, HttpServletRequest request, Model model) {
		int mixId = (Integer) request.getAttribute("mixId");
		model.addAttribute("solutionFaceProductMixDetail", solutionFaceProductMixDetail);
		model.addAttribute("mixId", mixId);
		return "modules/expert/solutionFaceProductMixDetailForm";
	}
	
	@RequestMapping(value = "save")
	public String save(SolutionFaceProductMixDetail de, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		SolutionFaceProductMixDetail old = solutionFaceProductMixDetailService.getBySolutionFaceProductMixDetail(de);
		if(old != null) {
			old.setMix_ratio(de.getMix_ratio());
			de = old;
		}
		solutionFaceProductMixDetailService.save(de);
		Long id = de.getSolutionFaceProductMix().getId();
		Long stepId = solutionFaceProductMixService.get(id).getSolutionFaceProductStep().getId();
		addMessage(redirectAttributes, "保存 方案产品步骤详细成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceProductMix/form/?stepId=" + stepId +"&id=" + id;
	}
	
	@RequestMapping(value = "delete")
	public String delete(SolutionFaceProductMixDetail de, RedirectAttributes redirectAttributes) {
		Long id = de.getSolutionFaceProductMix().getId();
		Long stepId = de.getSolutionFaceProductMix().getSolutionFaceProductStep().getId();
		solutionFaceProductMixDetailService.delete(de);
		addMessage(redirectAttributes, "删除 方案产品步骤详细成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceProductMix/form/?stepId=" + stepId +"&id=" + id;
	}

}
